public class Test15_synchronized {
    public static void main(String[] args) {
        SellTickOp sto=new SellTickOp(10);
        for (int i=1;i<=12;i++){
            Thread counter1=new Thread(sto,"柜员===");
            counter1.start();
        }
    }
}
class SellTickOp implements Runnable{
int tickets;//总票数
    public SellTickOp(int tickets){
        this.tickets=tickets;
    }
    @Override
    public void run() {
        while(true){
            //块级：将同步加到要同步的代码上 注意 synchronized（参照对象）{}
            if (tickets>0){
                System.out.println(Thread.currentThread().getName()+"在sell第"+(tickets--)+"张票");
                try {
                    Thread.sleep(1900);
                    //wait(r.nextInt(800));//必须与 synchronized结合使用
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else {return;}
        }

    }
}
